
class UserData {
  nickname = ""; // 用户昵称
  address = ""; // 用户地址
  connection; // WebSocket连接

  constructor(nickname, address, connection) {
    this.nickname = nickname;
    this.address = address;
    this.connection = connection; // WebSocket connection
  }

  getID() {
    return `${this.nickname}@${this.address}`;
  }
}

const livingUserMap = new Map();
module.exports = {
  UserData,
  addLivingUser(user) {
    livingUserMap.set(user.getID(), user);
  },
  removeLivingUser(user) {
    livingUserMap.delete(user.getID());
  },
  getLivingUserMap() {
    return livingUserMap;
  },
  getLivingUsers(getConnection) {
    if (getConnection) {
      return Array.from(livingUserMap.values());
    }
    const arr = [];
    for (const u of livingUserMap.values()) {
      arr.push({
        id: u.getID(),
        nickname: u.nickname,
        address: u.address
      });
    }
    return arr;
  },
}
